package cn.edu.tjufe.llp.tin.controller.part01;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * [Springboot的static和templates](https://www.cnblogs.com/wangshen31/p/8727359.html)
 * [Model, ModelMap, and ModelView in Spring MVC](https://www.baeldung.com/spring-mvc-model-model-map-model-view)
 * 
 * @author llp
 *
 */
@Controller
@RequestMapping("/part01")
public class TemplateController {
	
    @RequestMapping("/goToViewHello")
    public ModelAndView sayHello() {
        ModelAndView modelAndView = new ModelAndView();

        // @RequestMapping("/part01")改变了当前目录。
    	// 所以setViewName字符串最前面的“/”必须加上。
        modelAndView.setViewName("/html/part01/hello");
        modelAndView.addObject("key", 12345);
        return modelAndView;
    }
    
    @GetMapping("/goToViewPage")
    public ModelAndView passParametersWithModelAndView() {
        ModelAndView modelAndView = new ModelAndView("/html/part01/viewPage");
        modelAndView.addObject("message", "Baeldung");
        return modelAndView;
    }    
    
    @GetMapping("/showViewPage")
    public String passParametersWithModel(Model model) {
        Map<String, String> map = new HashMap<>();
        map.put("spring", "mvc");
        model.addAttribute("message", "Baeldung");
        model.mergeAttributes(map);
        return "/html/part01/viewPage";
    }
    
    @GetMapping("/printViewPage")
    public String passParametersWithModelMap(ModelMap map) {
        map.addAttribute("welcomeMessage", "welcome");
        map.addAttribute("message", "Baeldung");
        return "/html/part01/viewPage";
    }    
}
